package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.annotation.TokenPermission;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.constant.CommonConstant;
import com.kgc.scd.service.CouponServerService;
import com.kgc.scd.service.CouponWebService;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created On : 2022/11/15.
 * <p>
 * Author : yantao
 * <p>
 * Description: UserCouponController
 */
@Slf4j
@RestController
@RequestMapping("/yt")
@Api(tags = "用户优惠卷操作入口")
public class UserCouponController {

    @Autowired
    private CouponServerService couponServerService;

    @Autowired
    private CouponWebService couponWebService;

    @GetMapping("receiveCoupons")
    @TokenPermission
    @ApiOperation(value = "领取优惠券",notes = "有参,mybatis-plus技术")
    public RequestResult<?> receiveCoupons(@ApiParam(value = "优惠券id") @RequestParam String couponId,
                                           //@ApiParam(required = false) @RequestParam String categoryId,
                                           @ApiIgnore @RequestShopUser ShopUser shopUser
    ) {
        if (couponWebService.isExpiredCoupon(couponId)){
            return ResultBuildUtil.fail( "301","优惠券已经过期" );
        }
        if (couponWebService.judgingInventory(couponId)){
            return ResultBuildUtil.fail( "303","优惠券库存不足" );
        }


        if (couponWebService.isSufficientIntegration(couponId,shopUser.getId())){
            return ResultBuildUtil.fail( "302","积分不足,无法购买" );
        }





        return ResultBuildUtil.success("购买成功用户优惠券:"+couponServerService.convertCoupons(couponId,shopUser.getId()));

    }

    @GetMapping("useCoupons")
    @TokenPermission
    @ApiOperation(value = "判断能否使用优惠券,使用后的金额",notes = "有参,已使用,已过期,该商品不能使用该优惠券,没有到达优惠价,返回map集合,有优惠后金额,mybatis-plus技术")
    public RequestResult<?> testCoupon(@ApiParam(value = "用户优惠券id") @RequestParam String userCouponId,
                                       @ApiParam( value = "商品种类id",required = false) @RequestParam String categoryId,
                                       @ApiParam(value = "金额") @RequestParam Double money,
                                       @ApiIgnore @RequestShopUser ShopUser shopUser
    ) {

        return ResultBuildUtil.success(couponServerService.wouldUseCoupons(userCouponId,categoryId,money,shopUser.getId()));

    }

    @GetMapping("delCoupons")
    @TokenPermission
    @ApiOperation(value = "使用后,删除优惠券",notes = "需要用户优惠券id")
    public RequestResult<?> delCoupons(@ApiParam(value = "用户优惠券id") @RequestParam String userCouponId
    ) {

        if (couponServerService.deleteUserCoupon(userCouponId)){
            return ResultBuildUtil.success("删除成功");

        }
        return ResultBuildUtil.fail( "删除失败,还没有使用" );

    }



    @GetMapping("receiveCouponsAll")
    @TokenPermission
    @ApiOperation(value = "得到所有的优惠券",notes = "需要用户id")
    public RequestResult<?> receiveCouponsAll(@ApiIgnore @RequestShopUser ShopUser shopUser
    ) {

       return ResultBuildUtil.success( couponServerService.queryCouponsAll(shopUser.getId()));

    }

    @GetMapping("initialization")
    @TokenPermission
    @ApiOperation(value = "初始化redis")
    public RequestResult<?> initialization(@RequestParam Integer integration1,@RequestParam Integer num1,@RequestParam Integer integration2,@RequestParam Integer num2,
            @ApiIgnore @RequestShopUser ShopUser shopUser
    ) {
        couponWebService.initializationAll(integration1,num1,integration2,num2,shopUser.getId());
        return ResultBuildUtil.success( );

    }

    @GetMapping("UseCouponWeb")
    @TokenPermission
    @ApiOperation(value = "提交订单时,使用优惠券",notes = "需要用户优惠券id")
    public RequestResult<?> UseCouponWeb(@ApiParam(value = "用户优惠券id") @RequestParam String userCouponId
    ) {
        couponServerService.consumeCoupon(userCouponId);
        return ResultBuildUtil.success( );
    }



}
